package channel

import (
	"context"

	model0 "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/user/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type USDTWalletModel struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
}

func (m *USDTWalletModel) getDB() *gorm.DB {
	return m.db
}

func NewUSDTWalletModel(ctx context.Context, db *gorm.DB) *USDTWalletModel {
	return &USDTWalletModel{
		ctx:    ctx,
		db:     db,
		Logger: logx.WithContext(ctx),
	}
}

func (m *USDTWalletModel) UserUsdtWalletsDel(in *rpc.UserUsdtWalletsDelRequest) (*rpc.UserUsdtWalletsDelResponse, error) {
	tx := m.db.Where("id = ?", in.Id).Delete(&model.UserChannelUsdt{
		Model: model0.Model{
			ID: (in.Id),
		},
		UserId: in.UserId,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrDel
	}

	return &rpc.UserUsdtWalletsDelResponse{
		IsDel: true,
	}, nil
}

// ------------------user channel usdt ------------------
func (m *USDTWalletModel) UserUsdtWalletsInfo(in *rpc.UserUsdtWalletsInfoRequest) (*rpc.UserUsdtWalletsInfoResponse, error) {
	var userUsdtWalletInfos []*model.UserChannelUsdt
	var userUsdtWalletsList []*rpc.UserUsdtWalletsInfo

	// 查询所有用户银行信息
	db := m.db
	if in.UserId != 0 {
		db = db.Where("user_id = ?", in.UserId)
	}
	db.Find(&userUsdtWalletInfos)
	for _, userUsdtWallet := range userUsdtWalletInfos {
		userUsdtWalletsInfo := &rpc.UserUsdtWalletsInfo{
			Id:           int64(userUsdtWallet.ID),
			UserId:       userUsdtWallet.UserId,
			UsdtType:     userUsdtWallet.UsdtType,
			Address:      userUsdtWallet.Address,
			AddressAlias: userUsdtWallet.AddressAlias,
		}
		userUsdtWalletsList = append(userUsdtWalletsList, userUsdtWalletsInfo)
	}

	return &rpc.UserUsdtWalletsInfoResponse{
		UserUsdtWalletsInfoList: userUsdtWalletsList,
	}, nil
}

func (m *USDTWalletModel) UserUsdtWalletsInsert(in *rpc.UserUsdtWalletsInsertRequest) (*rpc.UserUsdtWalletsInsertResponse, error) {
	tx := m.db.Create(&model.UserChannelUsdt{
		UserId:       in.UserId,
		UsdtType:     in.UsdtType,
		Address:      in.Address,
		AddressAlias: in.AddressAlias,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrInsert
	}

	return &rpc.UserUsdtWalletsInsertResponse{
		IsInsert: true,
	}, nil
}

func (m *USDTWalletModel) UserUsdtWalletsPage(in *rpc.UserUsdtWalletsPageRequest) (*rpc.UserUsdtWalletsPageResponse, error) {
	var userChannelUsdtInfos []*model.UserChannelUsdt
	offset := in.Page.PageSize * (in.Page.PageNo - 1)

	// 查询所有用户银行信息
	query := m.db.Model(&model.UserChannelUsdt{})
	if in.Param.UserId != 0 {
		query = query.Where("user_id = ?", in.Param.UserId)
	}
	if in.Param.UsdtType != "" {
		query = query.Where("usdt_type = ?", in.Param.UsdtType)
	}
	if in.Param.Address != "" {
		query = query.Where("address = ?", in.Param.Address)
	}
	var total int64
	query = query.Count(&total).Limit(int(in.Page.PageSize)).Offset(int(offset))
	tx := query.Find(&userChannelUsdtInfos)

	var userUsdtWallets []*rpc.UserUsdtWalletsInfo
	if tx.Error != nil {
		return &rpc.UserUsdtWalletsPageResponse{
			DataList: userUsdtWallets,
			Total:    total,
		}, xerrors.ErrDataConvert
	}
	_ = copier.Copy(&userUsdtWallets, userChannelUsdtInfos)
	for i := 0; i < len(userUsdtWallets); i++ {
		userUsdtWallets[i].Id = userChannelUsdtInfos[i].Model.ID
	}
	return &rpc.UserUsdtWalletsPageResponse{
		DataList: userUsdtWallets,
		Total:    total,
	}, nil
}

func (m *USDTWalletModel) UserUsdtWalletsUpdate(in *rpc.UserUsdtWalletsUpdateRequest) (*rpc.UserUpdateUsdtWalletsResponse, error) {

	tx := m.db.Where("id = ?", in.Id).Updates(&model.UserChannelUsdt{
		UserId:       in.UserId,
		UsdtType:     in.UsdtType,
		Address:      in.Address,
		AddressAlias: in.AddressAlias,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrUpdate
	}

	return &rpc.UserUpdateUsdtWalletsResponse{
		IsUpdate: true,
	}, nil
}
